/**
 * @(#) ConsumoComplessivo.java
 */

package it.unibo.iss.domotica.consumptionControlling;

import java.util.Observable;
import org.apache.log4j.Logger;

public class TotalConsumption extends Observable
{
	private double currentConsumption;
	private double threshold;
	private boolean alert;
	private Logger logger = Logger.getLogger(TotalConsumption.class);
	
	public TotalConsumption(double threshold) {
		this.threshold = threshold;
		alert = false;
	}
	
	public boolean isAlert() {
		return alert;
	}
	
	public synchronized void setCurrentConsumption(double currentConsumption, boolean notify) {
		this.currentConsumption = currentConsumption;

		// Invia le notifiche solo se il consumo supera la soglia massima
		// e se non sono ancora state inviate le notifiche relative a quel
		// superamento. Quando il consumo complessivo rientra nei limiti,
		// al prossimo superamento verranno inviate nuove notifiche.
		if(this.currentConsumption <= threshold) {
			alert = false;
		}
		else if(this.currentConsumption > threshold && !alert) {
			alert = true;
			
			if(notify) {
				/* Usando con TotalConsumptionObserver */
//				sendNotify();
				
				/* Usando java.util.Observer */
				setChanged();
				notifyObservers();
			}
		}
	}
	
	public synchronized double getCurrentConsumption() {
		return currentConsumption;
	}
	
	public double getThreshold() {
		return threshold;
	}

}
